package controller;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import entity.DecisionLinesEvent;

import server.ClientState;
import server.IProtocolHandler;
import server.ServerThread;
import xml.Message;

/**
 * kick a user in round robin
 * @author Hanlin
 */
public class RemoveUserController implements IProtocolHandler {

	@Override
	public Message process(ClientState state, Message request) {
		entity.System system = entity.System.getInstance();
		
		Node removeUserRequest = request.contents.getFirstChild();
		NamedNodeMap map = removeUserRequest.getAttributes();
		String eventId = map.getNamedItem("id").getNodeValue();
		String userName  = map.getNamedItem("user").getNodeValue();
		
		DecisionLinesEvent event = system.getEvent(eventId);
		boolean completed = event.removeUser(userName);
		
		system.updateEvent(eventId,event);
		
		// Broadcast joinResponse to everyone else
		NamedNodeMap requestMap = request.contents.getAttributes();
		String response = Message.responseHeader(requestMap.getNamedItem("id").getNodeValue());
		response += "<kickResponse completed='"+completed+"' /></response>";
		ServerThread.broadcastMessage(new Message(response), eventId, state.id());
		
		System.out.println(response);
		return new Message(response);
	}

}

/* Relevant schema
<!--  Kick User Request [must be round robin] -->
<!-- _______________________ -->

<xs:element name='kickRequest'> 
  <xs:complexType>
    <!-- remove a user from this event -->
    <xs:attribute name='id'   type='xs:string' use='optional'/>

    <!-- remove user from this event id. -->
    <xs:attribute name='user'      type='xs:string' use='required'/>

  </xs:complexType>
</xs:element>

<!-- client receives this [in fact everyone gets this] -->
<xs:element name='kickResponse'>
  <xs:complexType>
    <xs:attribute name='completed'  type='xs:boolean' use='optional'/>
  </xs:complexType>
</xs:element>
*/